#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys, random, itertools, re
from lab4 import TuringMachine
from lab4 import eraser as eraser_states


chkword_d = [{'0':(1, '+', 'r'), # 0
              '1':(2, '+', 'r'),
              '#':(7, '#', 'r'), #accept
              '+':(9, '@', 'n')},
              # remember the next symbol

             {'0':(1, '0', 'r'), # 1
              '1':(1, '1', 'r'),
              '#':(3, '#', 'r'),
              '+':(1, '@', 'n')},
             {'0':(2, '0', 'r'), # 2
              '1':(2, '1', 'r'),
              '#':(4, '#', 'r'),
              '+':(2, '@', 'n')},
             # walk right

             {'0':(5, '+', 'l'), # 3
              '1':(9, '!', 'n'), #reject
              '#':(3, '@', 'n'),
              '+':(3, '+', 'r'),
              ' ':(9, ' ', 'n')},
             {'0':(9, '!', 'n'), # 4 #reject
              '1':(5, '+', 'l'),
              '#':(4, '@', 'n'),
              '+':(4, '+', 'r'),
              ' ':(9, ' ', 'n')},
             # check the symbol on the other side

             {'0':(6, '0', 'l'), # 5
              '1':(6, '1', 'l'),
              '#':(5, '#', 'l'),
              '+':(5, '+', 'l'),
              ' ':(7, ' ', 'r')},
             # walk back

             {'0':(6, '0', 'l'), # 6
              '1':(6, '1', 'l'),
              '#':(6, '#', 'n'),
              '+':(0, '+', 'R')},

             {'0':(9, '0', 'n'), # 7
              '1':(9, '1', 'n'),
              '+':(7, '+', 'r'),
              '#':(7, '#', 'r'),
              ' ':(8, ' ', 'l')}

             # 8 accept
             # 9 reject
            ]

if __name__ == '__main__':
    v = '-v' in sys.argv
    chkword = TuringMachine(chkword_d)
#    for tape in ('11000101011#11000101011',
#                 '1#1',
#                 '0#0',
#                 '110#1100',
#                 '11#1',
#                 '11#0',
#                 '11#011',
#                 '#',
#                 '11111111110000000000#11111111110000000000',
#                 '110011#110011'):
#        print tape
#        res = chkword.run(tape, verbose=v)
#        if res == True:
#            print 'accepted'
#        elif res == False:
#            print 'rejected'
#        else:
#            print 'unknown error'
    print 'testing random strings'
    for i in range(200):
        s = ''.join(random.choice('01') for j in range(random.randint(0, 12)))
        s = s + '#' + s
        if chkword.run(s, verbose=v):
            print '%s%saccepted' % (s, ' ' * (1 + 24 + 1 - len(s)))
            if not bool(re.search(r'^([01]*)#\1$', s)):
                print 'REGEXP TEST FAILED'
        else:
            print '%s%srejected' % (s, ' ' * (1 + 24 + 1 - len(s)))
            if bool(re.search(r'^([01]*)#\1$', s)):
                print 'REGEXP TEST FAILED'
    for i in range(200):
        s = ''.join(random.choice('01') for j in range(random.randint(0, 10))) + \
                '#' + \
                ''.join(random.choice('01') for j in range(random.randint(0, 10)))
        if chkword.run(s, verbose=v):
            print '%s%saccepted' % (s, ' ' * (1 + 20 + 1 - len(s)))
            if not bool(re.search(r'^([01]*)#\1$', s)):
                print 'REGEXP TEST FAILED'
        else:
            print '%s%srejected' % (s, ' ' * (1 + 20 + 1 - len(s)))
            if bool(re.search(r'^([01]*)#\1$', s)):
                print 'REGEXP TEST FAILED'
